//
//  NSDate+Time.m
//  CharonTVProject
//
//  Created by karong on 2023/6/29.
//

#import "NSDate+Time.h"

@implementation NSDate (Time)
+ (NSString *)novelUpdateStringWithMSTimestamp:(NSTimeInterval)timestamp {
    return [self dateStringWithMSTimestamp:timestamp format:@"yyyy-MM-dd"];
}

+ (NSString *)dateStringWithMSTimestamp:(NSTimeInterval)timestamp format:(NSString *)format {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:format];
    NSString * string = [dateFormat stringFromDate:date];
    return string;
}

+ (NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd HH:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    return timeSp;
}

+ (NSString *)createHisdoryShowTimeStrWithTimeStamp:(NSString *)timeStamp {
    NSString *showTimeStr;
    NSString *dateStr = [self convertNSStringWithDate:timeStamp format:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [NSDate dateWithString:dateStr format:@"yyyy-MM-dd HH:mm:ss"];
    NSString *nowDateStr = [[NSDate date] stringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *nowDate = [NSDate dateWithString:nowDateStr format:@"yyyy-MM-dd HH:mm:ss"];
    NSInteger chaNum = [self numberOfDaysWithFromDate:date toDate:nowDate]/24/60;
    if (chaNum == 0) {
        showTimeStr = WSLocalized(@"今天");
    }else {
        NSString *yearStr = [self convertNSStringWithDate:timeStamp format:@"yyyy-MM-dd"];
        showTimeStr = yearStr;
    }
    return showTimeStr;
}

+ (NSString *) convertNSStringWithDate:(NSString *) date format:(NSString * _Nullable)fmoratStr{
    NSDate *editDate = [NSDate dateWithTimeIntervalSince1970:date.integerValue];
    if ([fmoratStr isNotBlank]) {
        return [editDate stringWithFormat:fmoratStr];
    }
    return [editDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
}

+ (NSInteger)numberOfDaysWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate{
    NSCalendar *cal=[NSCalendar currentCalendar];
    unsigned int unitFlags= NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute;
    NSDateComponents *comp = [cal components:unitFlags fromDate:fromDate toDate:toDate options:0];
    NSInteger hor = comp.day * 24;
    NSInteger minHor = hor + comp.hour;
    NSInteger min = (minHor * 60) + comp.minute;
    return min;
}

/// 获取系统当前时间
/// @param formatStr YYYY-MM-dd HH:mm:ss
+(NSString*)getCurrentTimesFormat:(NSString *)formatStr {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatStr];
    NSDate *dateNow = [NSDate date];
    NSString *currentTime = [formatter stringFromDate:dateNow];
    return currentTime;
}
@end
